package com.ywgt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ywgt.pojo.Comment;
import com.ywgt.mapper.CommentMapper;
import com.ywgt.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论表 服务实现类
 * </p>
 *
 * @author yiping
 * @since 2021-03-13
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Override
    public Page<Comment> getComments(String userId, String valueId, int page, int limit, String sort, String order) {
        PageHelper.startPage(page,limit);
        return baseMapper.selectComments(userId,valueId,sort,order);
    }

    @Override
    public boolean reply(Map<String,Object> body) {
        Integer commentId = (Integer) body.get("commentId");
        String content = (String) body.get("content");
        Comment comment=new Comment();
        comment.setType(2);
        comment.setValueId(commentId);
        comment.setContent(content);
        comment.setUserId(0); // 评价回复没有用
        comment.setStar(0); // 评价回复没有用
        comment.setHasPicture(false); // 评价回复没有用
        comment.setPicUrls(new String[] {}); // 评价回复没有用
        if(this.save(comment)){
            return true;
        }
        return false;
    }

    @Override
    public Page<Comment> getCommentList(Integer valueId, Integer type, Integer showType, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        return baseMapper.selectCommentList(valueId,type,showType);
    }

    @Override
    public String getReply(Integer id) {
        LambdaQueryWrapper<Comment> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Comment::getValueId,id);
        wrapper.eq(Comment::getType,2);
        wrapper.eq(Comment::getDeleted,0);
        List<Comment> list = this.list(wrapper);
        if(list.size()>0){
            return list.get(0).getContent();
        }
        return null;
    }

    @Override
    public List<Comment> getByGoodsId(Integer id, int page, int limit) {
        LambdaQueryWrapper<Comment> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Comment::getValueId,id);
        wrapper.eq(Comment::getDeleted,0);
        wrapper.orderByDesc(Comment::getAddTime);
        PageHelper.startPage(page,limit);
        return baseMapper.selectList(wrapper);
    }
}
